﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace Common
{
    public enum ChannelType
    {
        Vibration =0,
    }

    /// <summary>
    /// 是否有效
    /// </summary>
    public enum ChannelStaute
    {
        Enable =0,
        Disable =1,
  
    }

    /// <summary>
    /// 量程档位
    /// </summary>
    public enum Gear 
    {
        OneGear,
        TwoGear,
        TheerGear,
    }

    /// <summary>
    /// 传感器类型
    /// </summary>
    public enum SensorType
    {
        Displacement,//位移
        Speed,//速度
        Acceleration,//加速度
    }

    /// <summary>
    /// 是否隔值
    /// </summary>
    public enum IsIsolation 
    {
        Yes,//是
        No,//否
    }



    /// <summary>
    /// 是否积分
    /// </summary>
    public enum IsCalculus
    {
        Yes,//是
        No,//否
    }



    public class Cls_Channels : BaseNode
    {
        public Cls_Channels(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }

        private List<Cls_Channel> _list = null;

        public List<Cls_Channel> Channels
        {
            get
            {
               // List<BaseNode> lst = new List<BaseNode>();
                Cls_Channel c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<Cls_Channel>();

                foreach (XmlNode x in _XmlNode.SelectNodes("Channel"))
                {
                    c = new Cls_Channel(x);
                  //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_Channel);
                //}
                _list.Sort();
                return _list;
            }
        }

        public Cls_Channel Channel(int id)
        {
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;

            //add new task
            ch = _XmlNode.SelectSingleNode("Channel[@id=" + id.ToString() + "]");
            if (ch == null) return null;

            return new Cls_Channel(ch);
        }

        public void Sort()
        {
 
        }
        //Add channel
        //添加时间：2014/8/1
        //添加人：洪伟刚
        //修改时间：2014/8/4
        //修改人：李福安
        public Cls_Channel AddChannel(int id,string channelname)
        {
            //find 
            XmlNode ch;
            XmlDocument doc = this._XmlNode.OwnerDocument;
            Cls_Channel ExistCh = Channel(id);
            if (null != ExistCh) return ExistCh;

            //add new task
            ch = _XmlNode.AppendChild(doc.CreateElement("Channel"));
            ch.Attributes.Append(doc.CreateAttribute("id")).Value = id.ToString();

            //set task property
            ch.AppendChild(doc.CreateElement("Name")).InnerText = channelname;

            ///通道名称  
            ch.AppendChild(doc.CreateElement("ChannelName")).InnerText = channelname;
            ///序列号
            ch.AppendChild(doc.CreateElement("SerialNumber")).InnerText = (id+1).ToString();
            ///是否有效
            ch.AppendChild(doc.CreateElement("ChannelStaute")).InnerText = "";
            ///安装角度
            ch.AppendChild(doc.CreateElement("InstallationAngle")).InnerText = "";
            ///量程档位
            ch.AppendChild(doc.CreateElement("Gear")).InnerText = "";
            ///通道单位
            ch.AppendChild(doc.CreateElement("Unit")).InnerText = "";
            ///传感器类型
            ch.AppendChild(doc.CreateElement("SensorType")).InnerText = "";

            ///偏移量
            ch.AppendChild(doc.CreateElement("OffsetValue")).InnerText = "";
            ///是否隔值
            ch.AppendChild(doc.CreateElement("IsIsolation")).InnerText = "";
            ///通道单位
            ch.AppendChild(doc.CreateElement("Unit")).InnerText = "";
            ///是否积分
            ch.AppendChild(doc.CreateElement("IsCalculus")).InnerText = "";
            ///灵敏度系数
            ch.AppendChild(doc.CreateElement("Sensitivity")).InnerText = "8.0";
            ///停机位置
            ch.AppendChild(doc.CreateElement("StopPosition")).InnerText = "";
            ///矫正系数
            ch.AppendChild(doc.CreateElement("Linearity")).InnerText = "";
            ///积分系数
            ch.AppendChild(doc.CreateElement("Calculus")).InnerText = "";
            ///零点偏移
            ch.AppendChild(doc.CreateElement("ZeroOffset")).InnerText = "";
            ///可选频段1名称
            ch.AppendChild(doc.CreateElement("Band1")).InnerText = "";
            ///可选频段2名称
            ch.AppendChild(doc.CreateElement("Band2")).InnerText = "";
            ///可选频段3名称
            ch.AppendChild(doc.CreateElement("Band3")).InnerText = "";
            ///可选频段1上限
            ch.AppendChild(doc.CreateElement("ULimit1")).InnerText = "";
            ///可选频段2上限
            ch.AppendChild(doc.CreateElement("ULimit2")).InnerText = "";
            ///可选频段3上限
            ch.AppendChild(doc.CreateElement("ULimit3")).InnerText = "";
            ///可选频段1下限
            ch.AppendChild(doc.CreateElement("LLimit1")).InnerText = "";
            ///可选频段2下限
            ch.AppendChild(doc.CreateElement("LLimit2")).InnerText = "";
            ///可选频段3下限
            ch.AppendChild(doc.CreateElement("LLimit3")).InnerText = "";

            ///慢信号类型
            ch.AppendChild(doc.CreateElement("SlowType")).InnerText = "";

            ch.AppendChild(doc.CreateElement("SpeedId")).InnerText = "1";

            //ch.AppendChild(doc.CreateElement("RecordEnable")).InnerText = "False";
            //ch.AppendChild(doc.CreateElement("ChannleType")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("ChannelStaute")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("InstallationPostion")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("SignalType")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("Unit")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("ULimit")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("LLimit")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("Direction")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("Magnification")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("Sensitivity")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("SensitivityUnit")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("OffSetValue")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("SensorType")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("WarningLine")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("CalculusValue")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("DangersLine")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("ChannelPropertyList")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("InstallationAngle")).InnerText = "";
            //ch.AppendChild(doc.CreateElement("ChannleType")).InnerText = "";



            return new Cls_Channel(ch);
        }



        public void UpdateChannel(int id,string name,string data) 
        {
            Cls_Channel ch = this.Channel(id);
            if (null == ch) return;
            XmlNode n = ch.Node.ParentNode;
            ch.Node.SelectSingleNode(name).InnerText = data;
        }

        public void DeleteChannel(int id)
        {
            Cls_Channel ch = this.Channel(id);
            if (null == ch) return;

            XmlNode n = ch.Node.ParentNode;
            n.RemoveChild(ch.Node);
        }
    }

    public class Cls_Channel : BaseNode
    {
        public Cls_Channel(XmlNode Node)
            : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _XmlNode = Node;
        }


      //  public double PK2PK = 0.0;//峰峰值 ，单位um
        public double GapVoltage = 0.0; // 间隙电压，单位mv
        public double RoomSquareValue = 0.0;//均方根值，单位um
        public double OneMultiplierAmp = 0.0;//一倍频幅值,单位um
        public double OneMultiplierPhase = 0.0;//一倍频相位，单位um
        public double TWOMultiplierAmp = 0.0;//二倍频幅值,单位um
        public double TWOMultiplierPhase = 0.0;//二倍频相位，单位um
        public double HalfMultiplierAmp = 0.0;//半频幅值，um
        public double FrequencyRange1 = 0.0;//可选频段1
        public double FrequencyRange2 = 0.0;//可选频段2
        public double FrequencyRange3 = 0.0;//可选频段3
        
        public Byte[] Initialdata = new byte[1024];//转速标示2个BYTE，原始数据2，短整型//存入数据库用image或者binary(50)

        public Cls_ChannelPropertyList ChannelPropertyList
        {
            get
            {
                XmlNode x = _XmlNode.SelectSingleNode("ChannelPropertyList");
                if (x == null)
                    x = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ChannelPropertyList"));
                // return null;
                return new Cls_ChannelPropertyList(x);
            }
        }

        public bool RecordEnable
        {
            get
            {
                try
                {

         
                if (_XmlNode == null)
                {
                    return false;
                }
                return (_XmlNode["RecordEnable"].InnerText == "1");
                }
                catch (Exception)
                {

                    return false;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("RecordEnable");

                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("RecordEnable"));

                Node.InnerText = value.ToString();
            }
        }

        public ChannelType ChannelType
        {
            get
            {
                return (ChannelType)Enum.Parse(typeof(ChannelType), _XmlNode["ChannelType"].InnerText, true);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ChannelType"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ChannelType"));
                _XmlNode["ChannelType"].InnerText = value.ToString();
            }
        }

      

     
        ///序号
        public string SerialNumber
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["SerialNumber"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("SerialNumber");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("SerialNumber"));
                Node.InnerText = value.ToString();
            }
        }


        ///通道名称
        public string ChannelName
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["ChannelName"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("ChannelName");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ChannelName"));
                Node.InnerText = value.ToString();
            }
        }
        ///是否有效
        public ChannelStaute ChannelStaute
        {
            get
            {
                return (ChannelStaute)Enum.Parse(typeof(ChannelStaute), _XmlNode["ChannelStaute"].InnerText, true);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["ChannelStaute"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("ChannelStaute"));
                _XmlNode["ChannelStaute"].InnerText = value.ToString();
            }
        }
        ///安装角度
        public float InstallationAngle
        {
            get
            {
                try
                {

                if (_XmlNode == null)
                {
                    return 0;
                }
                if (_XmlNode["InstallationAngle"].InnerText.Trim() != "")
                {
                    return float.Parse(_XmlNode["InstallationAngle"].InnerText);
                }
                else 
                {
                    return 0;
                }

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("InstallationAngle");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("InstallationAngle"));
                Node.InnerText = value.ToString();
            }
        }
        ///量程档位
        public Gear Gear
        {
            get
            {
                return (Gear)Enum.Parse(typeof(Gear), _XmlNode["Gear"].InnerText, true);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["Gear"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("Gear"));
                _XmlNode["Gear"].InnerText = value.ToString();
            }
        }
        ///通道单位
        public string Unit
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["Unit"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Unit");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Unit"));
                Node.InnerText = value.ToString();
            }
        }


        ///传感器类型
        public string SensorType
        {
            get
            {
                if (_XmlNode["SensorType"].InnerText == "")
                {
                    return "0";
                }
                else 
                {
                    return _XmlNode["SensorType"].InnerText;
                }
              
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["SensorType"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("SensorType"));
                _XmlNode["SensorType"].InnerText = value.ToString();
            }
        }
        ///是否隔值
        public IsIsolation IsIsolation
        {
            get
            {
                return (IsIsolation)Enum.Parse(typeof(IsIsolation), _XmlNode["IsIsolation"].InnerText, true);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["IsIsolation"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("IsIsolation"));
                _XmlNode["IsIsolation"].InnerText = value.ToString();
            }
        }
        ///是否积分
        public IsCalculus IsCalculus
        {
            get
            {
                return (IsCalculus)Enum.Parse(typeof(IsCalculus), _XmlNode["IsCalculus"].InnerText, true);
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["IsCalculus"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("IsCalculus"));
                _XmlNode["IsCalculus"].InnerText = value.ToString();
            }
        }
        ///灵敏度系数
        public float Sensitivity
        {
            get
            {
                try
                {

                if (_XmlNode == null||_XmlNode["Sensitivity"].InnerText=="")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["Sensitivity"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Sensitivity");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Sensitivity"));
                Node.InnerText = value.ToString();
            }
        }

        ///偏移量
        public float OffsetValue
        {
            get
            {
                try
                {

         
                if (_XmlNode == null || _XmlNode["OffsetValue"].InnerText=="")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["OffsetValue"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("OffsetValue");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("OffsetValue"));
                Node.InnerText = value.ToString();
            }
        }
        ///停机位置
        public string StopPosition
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["StopPosition"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("StopPosition");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("StopPosition"));
                Node.InnerText = value.ToString();
            }
        }

        ///线性度系数
        public float Linearity
        {
            get
            {
                try
                {

          
                if (_XmlNode == null || _XmlNode["Linearity"].InnerText=="")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["Linearity"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Linearity");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Linearity"));
                Node.InnerText = value.ToString();
            }
        }
        ///积分系数
        public float Calculus
        {
            get
            {
                try
                {

                if (_XmlNode == null || _XmlNode["Calculus"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["Calculus"].InnerText);

                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Calculus");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Calculus"));
                Node.InnerText = value.ToString();
            }
        }
        ///零点偏移
        public float ZeroOffset
        {
            get
            {
                try
                {

          
                if (_XmlNode == null || _XmlNode["ZeroOffset"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["ZeroOffset"].InnerText);
                }
                catch (Exception)
                {

                    return 0;
                }
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("ZeroOffset");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ZeroOffset"));
                Node.InnerText = value.ToString();
            }
        }

        ///可选频段1名称
        public string Band1
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["Band1"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Band1");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Band1"));
                Node.InnerText = value.ToString();
            }
        }
        ///可选频段2名称
        public string Band2
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["Band2"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Band2");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Band2"));
                Node.InnerText = value.ToString();
            }
        }
        ///可选频段3名称
        public string Band3
        {
            get
            {
                if (_XmlNode == null)
                {
                    return "";
                }
                return _XmlNode["Band3"].InnerText;
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("Band3");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("Band3"));
                Node.InnerText = value.ToString();
            }
        }
        /// <summary>
        /// 跟随转速
        /// </summary>
        public string SpeedId
        {
            get
            {
                try
                {
                    if (_XmlNode["SpeedId"] != null)
                    {
                        return _XmlNode["SpeedId"].InnerText;
                    }
                    else 
                    {
                        return "1";
                    }
                }
                catch (Exception)
                {

                    return "1";
                }
            }
            set
            {
                XmlDocument doc = _XmlNode.OwnerDocument;
                XmlNode x = _XmlNode["SpeedId"];
                if (null == x)
                    x = _XmlNode.AppendChild(doc.CreateElement("SpeedId"));
                _XmlNode["SpeedId"].InnerText = value.ToString();
            }
        }

        ///上限1
        public float ULimit1
        {
            get
            {
                if (_XmlNode == null || _XmlNode["ULimit1"].InnerText=="")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["ULimit1"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("ULimit1");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ULimit1"));
                Node.InnerText = value.ToString();
            }
        }

        ///上限2
        public float ULimit2
        {
            get
            {
                if (_XmlNode == null || _XmlNode["ULimit2"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["ULimit2"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("ULimit2");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ULimit2"));
                Node.InnerText = value.ToString();
            }
        }
        ///上限3
        public float ULimit3
        {
            get
            {
                if (_XmlNode == null || _XmlNode["ULimit3"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["ULimit3"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("ULimit3");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("ULimit3"));
                Node.InnerText = value.ToString();
            }
        }

        ///下限1
        public float LLimit1
        {
            get
            {
                if (_XmlNode == null || _XmlNode["LLimit1"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["LLimit1"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("LLimit1");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("LLimit1"));
                Node.InnerText = value.ToString();
            }
        }
        ///下限2
        public float LLimit2
        {
            get
            {
                if (_XmlNode == null || _XmlNode["LLimit2"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["LLimit2"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("LLimit2");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("LLimit2"));
                Node.InnerText = value.ToString();
            }
        }
        ///下限3
        public float LLimit3
        {
            get
            {
                if (_XmlNode == null || _XmlNode["LLimit3"].InnerText == "")
                {
                    return 0;
                }
                return float.Parse(_XmlNode["LLimit3"].InnerText);
            }
            set
            {
                XmlNode Node = _XmlNode.SelectSingleNode("LLimit3");
                if (Node == null)
                    Node = _XmlNode.AppendChild(_XmlNode.OwnerDocument.CreateElement("LLimit3"));
                Node.InnerText = value.ToString();
            }
        }
    }
}
